package com.labot.webapp.dwr.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import com.labot.dao.AuditDao;
import com.labot.model.User;
import com.labot.service.UserManager;
import com.labot.types.AuditEventType;
import com.labot.types.AuditType;
import com.labot.webapp.dwr.DwrService;
import com.labot.webapp.filter.InvalidateFilter;

/**
 * Implementation of Dwr interface.
 * 
 * @author Ing. Enrique Barbeito.
 * 
 */
@Service("dwrService")
public class DwrServiceImpl  implements DwrService {

	private static String  messageFromAdmin = null;
	private static Boolean showLoginMessageFromAdmin = false;
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private AuditDao audit;

	@Override
	public String test() {
		return "test";
	}
	
	@SuppressWarnings("unused")
	private HttpServletRequest getRequest() {
		WebContext ctx = WebContextFactory.get();
		return ctx.getHttpServletRequest();
	}
	
	private Boolean isAnonymousUser() {
		String username = SecurityContextHolder.getContext().getAuthentication().getName().toString();
		if (username.contains("anonymous")) {
			return true;
		}
		return false;
	}

	@Override
	public String invalidateUserSession(String userName) {
		User user = userManager.getUserByUsername( SecurityContextHolder.getContext().getAuthentication().getName().toString() );
		if (user.isAdmin()) {	 		
			InvalidateFilter.addUserToInvalidate(userName);
			audit.log(AuditEventType.INVALIDATE_USER_SESSION, AuditType.INFO, this.getClass().getSimpleName(), user.getUsername() , "" + user.getUsername() + " kick user -> " + userName);
			return "success";
		}
		return "forbidden";
	}
	
	@Override
	public String nameUserSession(String userName) {
		String username = SecurityContextHolder.getContext().getAuthentication().getName().toString();
		if (!username.contains("anonymous")) {
			User user = userManager.getUserByUsername(SecurityContextHolder.getContext().getAuthentication().getName().toString());
			if (user!= null) {	 		
	                  return user.getFullName();  
			}
		return " ";
		}
		return " ";
	}

	@Override
	public Map<String, Object> getMessageFromAdmin() {
		Map<String, Object> result = new HashMap<String, Object>();
		if (isAnonymousUser() && showLoginMessageFromAdmin || !isAnonymousUser()) {
			result.put("message", messageFromAdmin);
			result.put("login", showLoginMessageFromAdmin);
		} 
		return result;

	}

	@Override
	public void writeMessageFromAdmin(String message, Boolean login) {
		User user = userManager.getUserByUsername( SecurityContextHolder.getContext().getAuthentication().getName().toString() );
		if (user.isAdmin()) {			
			if (login == null) {
				login = true;
			} 
			showLoginMessageFromAdmin = login;
			messageFromAdmin = message;
			audit.log(AuditEventType.ADMIN_MESSAGE, AuditType.INFO, this.getClass().getSimpleName(), user.getUsername(), "" + message);
		}
	}
	
	
	
}
